﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class UserController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: User
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Search(int page, int limit, string name)
        {
            db.Configuration.LazyLoadingEnabled = false;


            var q = db.User.Where(p => true);
            if (name != "" && name != null)
            {
                q = q.Where(p => p.name.Contains(name));
            }

            var count = q.Count();//得到总统计数
            //根据时间排序，并实现分页
            var list = q.OrderByDescending(x => x.id).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                id = x.id,
                name = x.name,
                password=x.password,
                phone=x.phone,
                status=x.status,
                sex=x.sex,
                avatar=x.avatar,

            });
            return Json(new { code = 0, msg = "", count = count, data = list });
        }

        public ActionResult UserUpdate(int uid) {
            User user= db.User.SingleOrDefault(x=>x.id==uid);
            return View(user);
        }

        public ActionResult update(User user)
        {
            var userInfo= db.User.SingleOrDefault(x=>x.id==user.id);

            userInfo.name = user.name;
            userInfo.phone = user.phone;
            userInfo.status = user.status;
            userInfo.sex = user.sex;

            int row= db.SaveChanges();
            if (row > 0)
            {
                return Json(new { code = 0, msg = "更新成功!" });
            }
            else
            {
                return Json(new { code = -1, msg = "更新失败!" });
            }
        }

        public ActionResult Delect(int id)
        {
            User user= db.User.SingleOrDefault(x=>x.id==id);
            db.User.Remove(user);
            db.SaveChanges();
            return Json(new { code = 0, msg = "删除成功!" });
        }

    }
}